package player

import (
	"strconv"
	. "test_client/logic/internal"
)

type playerCommand struct {
}

var command = new(playerCommand)

func init() {
	Skeleton.AfterInit(func() {
		Skeleton.RegisterCommand("player.login", "input account password", command.login)
		Skeleton.RegisterCommand("player.logout", "player logout", command.logout)
		Skeleton.RegisterCommand("player.set_level", "set player level", command.setLevel)
		Skeleton.RegisterCommand("player.set_name", "set player name", command.setName)
	})
}

func (*playerCommand) login(args []interface{}) (result interface{}) {
	if len(args) < 1 {
		result = "need account"
		return
	}

	account := args[0].(string)
	rpc.Login(account, "")

	return
}

func (*playerCommand) logout(args []interface{}) (result interface{}) {
	rpc.Logout()

	return
}

func (*playerCommand) setLevel(args []interface{}) (result interface{}) {
	if len(args) < 1 {
		result = "need level"
		return
	}

	var text string
	var level uint16

	text = args[0].(string)
	if value, err := strconv.ParseUint(text, 10, 16); err != nil {
		result = err
		return
	} else {
		level = uint16(value)
	}

	rpc.TestSetLevel(level)

	return
}

func (*playerCommand) setName(args []interface{}) (result interface{}) {
	if len(args) < 1 {
		result = "need name"
		return
	}

	name := args[0].(string)
	rpc.TestSetName(name)

	return
}
